/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
    vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
        vector<vector<int> > ans;
        vector<int> tmp;
        vector<TreeNode *> now, last;
        if (root!=NULL) now.push_back(root);
        while (!now.empty()) {
            last.swap(now);
            now.clear();
            tmp.clear();
            for (int i=0;i<last.size();i++) {
                tmp.push_back(last[i]->val);
                if (last[i]->left!=NULL) now.push_back(last[i]->left);
                if (last[i]->right!=NULL) now.push_back(last[i]->right);
            }
            ans.push_back(tmp);
        }
        for (int i=1;i<ans.size();i+=2)
            for (int j=0;j<ans[i].size()/2;j++)
                swap(ans[i][j], ans[i][ans[i].size()-1-j]);
        return ans;
    }
};
